package com.hqs.rxjava;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;

import com.hqs.common.utils.Log;
import com.jakewharton.rxbinding2.view.RxView;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jakewharton.rxbinding2.widget.TextViewTextChangeEvent;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.BiFunction;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

import static android.util.Patterns.EMAIL_ADDRESS;
import static android.util.Patterns.PHONE;

public class ValidateActivity extends AppCompatActivity {

    @BindView(R.id.et_email)
    EditText etEmail;

    @BindView(R.id.et_phone)
    EditText etPhone;

    @BindView(R.id.tv_result)
    TextView tvResult;

    Disposable mDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_validate);

        ButterKnife.bind(this);


        Observable.combineLatest(RxTextView.textChangeEvents(etEmail).skipInitialValue(),
                RxTextView.textChangeEvents(etPhone).skipInitialValue(),
                new BiFunction<TextViewTextChangeEvent, TextViewTextChangeEvent, String>() {

                    @Override
                    public String apply(@NonNull TextViewTextChangeEvent textViewTextChangeEvent, @NonNull TextViewTextChangeEvent textViewTextChangeEvent2) throws Exception {

                        CharSequence email = textViewTextChangeEvent.text();
                        CharSequence phone = textViewTextChangeEvent2.text();

                        String result = "";
                        if (EMAIL_ADDRESS.matcher(email).matches()){
                            result = "email is ok  ";
                        }
                        else{
                            etEmail.setError("email error");
                        }
                        if (PHONE.matcher(phone).matches()){
                            result += "phone is ok";
                        }
                        else{
                            etPhone.setError("phone Error");
                        }
                        return result;
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        mDisposable = d;
                    }

                    @Override
                    public void onNext(@NonNull String result) {
                        tvResult.setText(result);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (mDisposable != null) {
            mDisposable.dispose();
        }
    }
}
